package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.apply.CancelApplyBService;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.zmn.business.interfaces.redistribute.RedistributeApplyBService;
import com.zmn.track.common.constant.apply.cancel.CancelApplyLogConsts;
import com.zmn.track.common.dto.apply.cancel.QueueApplyCancelDataDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author: wyc
 * @Description: 订单取消申请监听
 * @Date: 2022/5/25 21:48
 * @Version: 1.0
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.track}")
@Component
public class OrderCancelApplyListener {

    @Autowired
    private CancelApplyBService cancelApplyBService;
    @Autowired
    private RedistributeApplyBService redistributeApplyBService;


    /**
     * 取消申请节点通知
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.CANCEL_APPLY_NODE_NOTICE_MQ_TAG)
    public void cancelApplyNodeNotice(String message){
        log.info("取消申请节点通知 message={}", message);
        QueueApplyCancelDataDTO messageDto = JSON.parseObject(message, QueueApplyCancelDataDTO.class);
        try {
            // 变更待取消状态
            cancelApplyBService.changeWaitCancelStatus(messageDto);

            // 改派流程处理-取消申请发起时改派申请需要做对应处理-目前只需针对发起做处理
            if (Objects.equals(CancelApplyLogConsts.CANCEL_LOG_TYPE_APPLY, messageDto.getNodeType())) {
                redistributeApplyBService.cancelApplyHandle(messageDto);
            }
        } catch (Exception e) {
            log.info("{} 取消申请节点通知异常={}", messageDto.getWorkId(), e);
            e.printStackTrace();
        }
    }
}
